# PowerShell script to build HOMEBOX for ARMV7 platform only
Write-Host "Building HOMEBOX for ARMV7 platform..." -ForegroundColor Green

# Ensure dist directory exists
if (!(Test-Path "dist")) {
    New-Item -ItemType Directory -Path "dist" | Out-Null
}

# Build frontend if not already built
if (!(Test-Path "frontend\.output\public")) {
    Write-Host "Building frontend..." -ForegroundColor Yellow
    Set-Location "frontend"
    & pnpm run build
    Set-Location ".."
}

# Copy frontend files
Write-Host "Copying frontend files..." -ForegroundColor Yellow
if (!(Test-Path "backend\app\api\static\public")) {
    New-Item -ItemType Directory -Path "backend\app\api\static\public" | Out-Null
}
Copy-Item -Path "frontend\.output\public\*" -Destination "backend\app\api\static\public" -Recurse -Force

# Generate API docs
Write-Host "Generating API documentation..." -ForegroundColor Yellow
Set-Location "backend"
& swag fmt --dir=./app/api
& swag init --dir=./app/api,./internal/core/services,./internal/data/repo --parseDependency --output ./app/api/static/docs

# Build settings
$env:CGO_ENABLED = "0"
$env:GOOS = "linux"
$env:GOARCH = "arm"
$env:GOARM = "7"

Write-Host "Building linux armv7..." -ForegroundColor Cyan

$outputPath = "../dist/homebox-linux-armv7"

# Build ARMv7 with proper environment variables
$oldCgo = $env:CGO_ENABLED
$oldGoos = $env:GOOS
$oldGoarch = $env:GOARCH
$oldGoarm = $env:GOARM

try {
    $env:CGO_ENABLED = "0"
    $env:GOOS = "linux"
    $env:GOARCH = "arm"
    $env:GOARM = "7"
    
    # Build with minimal dependencies to avoid purego issues
    Write-Host "Building with CGO_ENABLED=0 for ARMv7..." -ForegroundColor Yellow
    go build -ldflags="-s -w" -tags="osusergo netgo static_build" -o "$outputPath" ./app/api
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host "鉂?linux armv7 build successful!" -ForegroundColor Green
    } else {
        Write-Host "鉂?linux armv7 build failed - trying alternative approach..." -ForegroundColor Red
        
        # Try building without problematic dependencies
        Write-Host "Attempting build with minimal flags..." -ForegroundColor Yellow
        go build -ldflags="-s -w" -o "$outputPath" ./app/api
        
        if ($LASTEXITCODE -eq 0) {
            Write-Host "鉂?linux armv7 build successful with minimal flags!" -ForegroundColor Green
        } else {
            Write-Host "鉂?linux armv7 build failed with all attempts" -ForegroundColor Red
            exit 1
        }
    }
} finally {
    # Restore environment variables
    $env:CGO_ENABLED = $oldCgo
    $env:GOOS = $oldGoos
    $env:GOARCH = $oldGoarch
    $env:GOARM = $oldGoarm
}

if ($LASTEXITCODE -eq 0) {
    Write-Host "✅ linux armv7 build complete" -ForegroundColor Green
} else {
    Write-Host "❌ linux armv7 build failed" -ForegroundColor Red
}

Set-Location ".."

Write-Host "`n🎉 ARMV7 build complete!" -ForegroundColor Green
Write-Host "Built binary:" -ForegroundColor Yellow
Get-ChildItem "dist\*" | ForEach-Object { Write-Host "  $($_.Name) - $([math]::Round($_.Length/1MB, 2)) MB" }